package com.xiaohu.transfrom.streamtranform;

import com.xiaohu.bean.WaterSensor;
import com.xiaohu.transfrom.MyPartitioner;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/*
    侧输出流，实现分流

    需求：WaterSensor,按照id分两种数据流 s1和s2
 */
public class SideOutputDemo {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        env.setParallelism(2);
        //设置流处理环境还是批处理环境 DataSet API已经过时了，现在都是一套代码，进行设置
//        env.setRuntimeMode(RuntimeExecutionMode.BATCH); //批处理
//        env.setRuntimeMode(RuntimeExecutionMode.STREAMING); //流处理，默认就是流处理
        //一般情况下，不会在代码中指定，不够灵活，一般都是在提交的时候，使用命令进行指定 flink run  -Dexecution.runtime-mode=BATCH【STREAMING】 ...

        DataStreamSource<String> socketDS = env.socketTextStream("master", 7777);

        SingleOutputStreamOperator<WaterSensor> waterSensorOpt1 = socketDS.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] datas = value.split(",");
                String id = datas[0];
                long ts = Long.parseLong(datas[1]);
                int vc = Integer.parseInt(datas[2]);
                return new WaterSensor(id, ts, vc);
            }
        });

        SingleOutputStreamOperator<WaterSensor> processOpt = waterSensorOpt1.process(new ProcessFunction<WaterSensor, WaterSensor>() {
            @Override
            public void processElement(WaterSensor value, ProcessFunction<WaterSensor, WaterSensor>.Context ctx, Collector<WaterSensor> out) throws Exception {
                String id = value.getId();
                if ("s1".equals(id)) {
                    //如果是s1放入到s1侧流中 
                    //OutputTag(String id, TypeInformation<T> typeInfo)
                    /*
                        第一个参数：侧输出流的标签名，自定义
                        第二个参数：放入到该流中的数据类型
                     */
                    OutputTag<WaterSensor> s1 = new OutputTag<>("s1", Types.POJO(WaterSensor.class));
                    //output(OutputTag<X> outputTag, X value);
                    /*
                        第一个参数：Tag对象
                        第二个参数：放入侧输出流的数据
                     */
                    ctx.output(s1, value);
                } else if ("s2".equals(id)) {
                    //如果是s2放入到s2侧流中
                    OutputTag<WaterSensor> s1 = new OutputTag<>("s2", Types.POJO(WaterSensor.class));
                    //output(OutputTag<X> outputTag, X value);
                    ctx.output(s1, value);
                } else {
                    //其他值放入到主流中
                    //out 处理的就是主流
                    out.collect(value);

                }
            }
        });

        processOpt.print("主流："); //直接打印的是主流的数据

        //打印侧输出流
        SideOutputDataStream<WaterSensor> sideStreamS1 = processOpt.getSideOutput(new OutputTag<WaterSensor>("s1", Types.POJO(WaterSensor.class)));
        sideStreamS1.print("s1流：");
        SideOutputDataStream<WaterSensor> sideStreamS2 = processOpt.getSideOutput(new OutputTag<WaterSensor>("s2", Types.POJO(WaterSensor.class)));
        sideStreamS2.print("s2流：");


        env.execute();
    }
}
